<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 商品模型
 *
 * @author      Eden
 * @datetime    2016/12/14 20:27
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Unit_Model extends ADM_Model
{

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 删除
     * @param $unitId
     * @return bool
     */
    public function removeUnit($unitId)
    {
        try {
            $this->db->where('id', $unitId);
            $this->db->update('base_unit', array('status' => -1));
            return $this->db->affected_rows();
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 获取所有单位
     * @return bool
     */
    public function getLists()
    {
        try {
            $this->db->where('status != -1');
            $this->db->order_by('id', 'DESC');
            $q = $this->db->get('base_unit');

            if ($rows = $q->result_array()) foreach($rows as &$row) {
                $row['id'] = intval($row['id']);
                $row['status'] = intval($row['status']);
            }
            return $rows;
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 添加单位
     * @param $name
     * @return array|bool
     */
    public function addUnit($name)
    {
        $data = array(
            'name' => $name,
            'status' => 1
        );
        $this->db->insert('base_unit', $data);
        $lastId = $this->db->insert_id();
        if ($lastId) {
            return array_merge($data, array('id' => intval($lastId)));
        }
        return false;
    }

}
